package com.qyer.util.log.service.impl;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

import com.qyer.util.log.common.Constants;
import com.qyer.util.log.entity.AppTrackLog;
import com.qyer.util.log.entity.ArsalNginxLog;
import com.qyer.util.log.entity.NginxLog;
import com.qyer.util.log.util.AppLogUtil;
import com.qyer.util.log.util.HiveFormatUtil;
import com.qyer.util.log.util.MyDateUtil;


/** 
* 此类描述的是： AppTrackServiceImpl接口实现类
* @author: hongxi.sun@qyer.com 
* @version: 2015年9月21日 上午10:00:24 
*/
public class AppTrackServiceImpl {
	/** 
	* 此方法描述的是： 清洗 track nginxlog
	* @author: hongxi.sun@qyer.com 
	* @version: 2015年9月21日 上午10:00:24 
	* @param  String line 
	* @return  TrackLog  格式有问题时返回 null
	*/
	public AppTrackLog getTrack4NginxFormat(String line) {
		NginxLog nginxLog = AppLogUtil.getNginxLog(line);
		if (nginxLog == null) {
			return null;
		}
		AppTrackLog trackLog = new AppTrackLog();
		if(!AppLogUtil.initBaseLog(nginxLog,trackLog)){
			return null ;
		}
		
		if(trackLog.getDeviceid()==null){
			return null ; 
		}
		
		Map<String, String> urlParamMaps = trackLog.getParamsMap();
		if (urlParamMaps == null||urlParamMaps.size()==0) {
			return null;
		}
		
		Map<String, String> extParamMaps = trackLog.getParamsMap();
		extParamMaps.remove("track_user_id");
		extParamMaps.remove("lat");
		extParamMaps.remove("lon");
		extParamMaps.remove("track_app_version");
		extParamMaps.remove("installtime");
		
		extParamMaps.remove("info");
		extParamMaps.remove("track_device_info");
		extParamMaps.remove("ip");
		extParamMaps.remove("client_secret");
		extParamMaps.remove("track_app_channel");
		extParamMaps.remove("track_os");
		extParamMaps.remove("client_id");
		extParamMaps.remove("track_deviceid");
		
		extParamMaps.remove("api_name");
		extParamMaps.remove("oauth_token");
		extParamMaps.remove("timestamp");
		//extParamMaps.remove("timestamp");
		trackLog.setExtParamMap(extParamMaps);
		
		return trackLog;
	}
	
	/** 
	* 此方法描述的是： 清洗 track nginxlog
	* @author: hongxi.sun@qyer.com 
	* @version: 2015年9月21日 上午10:00:24 
	* @param  String line 
	* @return  TrackLog  格式有问题时返回 null
	*/
	public AppTrackLog getArsal4NginxFormat(String line) {
		ArsalNginxLog nginxLog = AppLogUtil.getArsalNginxLog(line);
		if (nginxLog == null) {
			return null;
		}
		AppTrackLog trackLog = new AppTrackLog();
//		if(!AppLogUtil.initBaseLog(nginxLog,trackLog)){
//			return null ;
//		}
		
		if(trackLog.getDeviceid()==null){
			return null ; 
		}
		
		Map<String, String> urlParamMaps = trackLog.getParamsMap();
		if (urlParamMaps == null||urlParamMaps.size()==0) {
			return null;
		}
		
		Map<String, String> extParamMaps = trackLog.getParamsMap();
		extParamMaps.remove("track_user_id");
		extParamMaps.remove("lat");
		extParamMaps.remove("lon");
		extParamMaps.remove("track_app_version");
		extParamMaps.remove("installtime");
		
		extParamMaps.remove("info");
		extParamMaps.remove("track_device_info");
		extParamMaps.remove("ip");
		extParamMaps.remove("client_secret");
		extParamMaps.remove("track_app_channel");
		extParamMaps.remove("track_os");
		extParamMaps.remove("client_id");
		extParamMaps.remove("track_deviceid");
		
		extParamMaps.remove("api_name");
		extParamMaps.remove("oauth_token");
		extParamMaps.remove("timestamp");
		//extParamMaps.remove("timestamp");
		trackLog.setExtParamMap(extParamMaps);
		
		return trackLog;
	}

	/** 
	* 此方法描述的是： 清洗 tracklog 初始化  UGC 相关的数据  页面类型 频道  以及ugc_contents
	* @author: hongxi.sun@qyer.com 
	* @version: 2015年9月08日 上午10:00:24 
	* @param  TrackLog trackLog 
	* @return  String  格式有问题时返回 null
	*/
	public AppTrackLog setCurlUGC(AppTrackLog trackLog  ,Map <String , String> pagetypeMaps ,  
				Map <String , String> channelMaps , Map <String , String> cityMaps , Map <String, String > countryMaps) {
		
		return trackLog;
	}
	
	/**
	 * 正则匹配获取匹配的内容
	 * 
	 * @param regex
	 * @param url
	 * @return
	 */
	public boolean getRegExBool(String regex, String url) {
		boolean result = false;
		Pattern pat = Pattern.compile(regex);
		Matcher mat = pat.matcher(url);
		while (mat.find()) {
			result = true;
		}
		return result;
	}
	
	/**
	 * 正则匹配获取匹配的内容
	 * 
	 * @param regex
	 * @param url
	 * @return
	 */
	public String getRegEx(String regex, String url) {
		String result = "-,-";
		Pattern pat = Pattern.compile(regex);
		Matcher mat = pat.matcher(url);
		while (mat.find()) {
			if (mat.groupCount() > 0) {
				if (null != mat.group(1)) {
					result = mat.group(1);
				}
			}

		}
		return result;
	}

	/** 
	* 此方法描述的是： 针对清洗日志获取对象
	* @author: hongxi.sun@qyer.com 
	* @version: 2015年9月08日 上午10:00:24 
	* @param  String line 
	* @return  TrackLog  格式有问题时返回 null
	*/
	public AppTrackLog getTrackFormat(String strLine) {
		
		if (strLine == null || strLine.equals("")) {
			return null;
		}
		
		String [] ls = StringUtils.split(strLine , Constants.FS);
		
		if(ls .length<20){
			return null; 
		}

		AppTrackLog tracklog = new AppTrackLog();
		tracklog.setUserId(ls[0]);
		tracklog.setDeviceid(ls[1]);
		tracklog.setLon(ls[2]);
		tracklog.setLat(ls[3]);
		tracklog.setAppVersion(ls[4]);
		tracklog.setAppChannel(ls[5]);
		tracklog.setDeviceInfo(ls[6]);
		tracklog.setOs(ls[7]);
		tracklog.setAuthToken(ls[8]);
		tracklog.setTimeStamp(ls[9]);
		tracklog.setClientId(ls[10]);
		tracklog.setClientSecret(ls[11]);
		tracklog.setDeviceName(ls[12]);
		tracklog.setInstalltime(ls[13]);
		tracklog.setUserIp(ls[14]);
		tracklog.setApiName(ls[15]);
		tracklog.setApiPath(ls[16]);
		tracklog.setInfo(ls[17]);
		tracklog.setSource(ls[18]);
		tracklog.setHour(ls[19]);
		
		tracklog.setPlatform("app");
		
		
		String timestamp =MyDateUtil.toTimeStamp2(tracklog.getTrackTime());
		tracklog.setTimeStamp(timestamp);
		
		return tracklog ;
	}
}
